var partnerPage = avalon.define({
	$id: "partnerPage",
	uid: "",
	modules: {
		1: false,
		2: false,
		3: false,
		4: false,
		5: false
	},
	status: 0,
	projectCounts: 0,
	policyCounts:0,
	orderCounts:0,
	recordCounts: 0,
	integralOpen:0,
	level:0,
	salesIntegralOpen:0,
	salesName:'',
	provision:''
});

mui.ready(function() {
	var uid = $("#uid").val();
	partnerPage.uid = uid;
	if("" != uid) {
		mui.ajax('/partner/index.json', {
			data: {
				uid: partnerPage.uid
			},
			dataType: 'json',
			success: function(data) {
				partnerPage.modules = data.modules;
				partnerPage.projectCounts = data.projectCounts;
				partnerPage.recordCounts = data.recordCounts;
				partnerPage.policyCounts = data.policyCounts;
				partnerPage.orderCounts = data.orderCounts;
				partnerPage.status = data.status;
				partnerPage.integralOpen = data.integralOpen;
				partnerPage.salesIntegralOpen = data.salesIntegralOpen;
				partnerPage.salesName = data.salesName;
				partnerPage.level=data.level;
				i18nReady();
			}
		});
	}

	mui(".mui-grid-view").on('tap', '.scan', function() {
		if("" != partnerPage.uid) {
			if(!mui.hasPermissions("dahua://scan")){
				
			}else if(openIntegral()){
				
			}else if(partnerPage.level=='1'){
				mui.alert("", $.i18n.prop('noAuthortiyScan'));
			}else if(partnerPage.status == 1) {
				mui.openWindow({
					url: "dahua://scan"
				});
			} else {
				mui.alert("", $.i18n.prop('noAuthortiy'));
			}
		} else {
			mui.openWindow({
				url: "dahua://login"
			});
		}

	});

	mui(".mui-grid-view").on('tap', '.channelPolicy', function() {
		if("" != partnerPage.uid) {
			if(openIntegral()){
				
			}else if(partnerPage.status == 1) {
				var url = this.getAttribute('href');
				mui.openWindow({
					url: url
				});
			} else {
				mui.alert("", $.i18n.prop('noAuthortiy'));
			}

		} else {
			mui.openWindow({
				url: "dahua://login"
			});
		}

	});

	mui(".mui-grid-view").on('tap', '.scanRecord', function() {
		if("" != partnerPage.uid) {
			if(openIntegral()){
				
			}else if(partnerPage.status == 1) {
				var url = this.getAttribute('href');
				mui.openWindow({
					url: url
				});
			} else {
				mui.alert("", $.i18n.prop('noAuthortiy'));
			}
		} else {
			mui.openWindow({
				url: "dahua://login"
			});
		}

	});

	mui(".mui-grid-view").on('tap', '.myProject', function() {
		// Ryan
		if(!window.DAHUA_CONFIG.isTest) {
			mui.toast('In development');
			return;
		}
		if("" != partnerPage.uid) {
			/* if(openIntegral()){
				
			}else */if(partnerPage.status == 1) {
				var url = this.getAttribute('href');
				mui.openWindow({
					url: url
				});
			} else {
				mui.alert("", $.i18n.prop('noAuthortiy'));
			}
		} else {
			mui.openWindow({
				url: "dahua://login"
			});
		}

	});
	mui(".mui-grid-view").on('tap', '.bounsMarket', function() {
		if("" != partnerPage.uid) {
			if(openIntegral()){
				
			}else if(partnerPage.status == 1) {
				var url = this.getAttribute('href');
				mui.openWindow({
					url: url
				});
			} else {
				mui.alert("", $.i18n.prop('noAuthortiy'));
			}
		} else {
			mui.openWindow({
				url: "dahua://login"
			});
		}

	});
	mui(".mui-grid-view").on('tap', '.cashCoupon', function() {
		// Ryan
		if(!window.DAHUA_CONFIG.isTest) {
			mui.toast('In development');
			return;
		}
		if("" != partnerPage.uid) {
			if(openIntegral()){
				
			}else if(partnerPage.status == 1) {
				var url = this.getAttribute('href');
				mui.openWindow({
					url: url
				});
			} else {
				mui.alert("", $.i18n.prop('noAuthortiy'));
			}
		} else {
			mui.openWindow({
				url: "dahua://login"
			});
		}

	});
	//我订单
	mui(".mui-grid-view").on('tap', '.order', function() {
		if("" != partnerPage.uid) {
			if(partnerPage.status == 1) {
				var url = this.getAttribute('href');
				
				mui.ajax("/user/getIsAgree.do", {
					data: {
						"uid": partnerPage.uid,
					},
					success: function(data) {
						var status = data.user.isAgree;
						if(status == 0) {
							mui.openWindow({
								url: "statements.html"
							});
						}else{
							mui.openWindow({
								url: url
							});
						}
					}
				});
			} else {
				mui.alert("", $.i18n.prop('noAuthortiy'));
			}
		} else {
			mui.openWindow({
				url: "dahua://login"
			});
		}

	});
	
	mui(".mui-grid-view").on('tap', '.statements', function() {
		if("" != partnerPage.uid) {
			if(partnerPage.status == 1) {
				var url = this.getAttribute('href');
				mui.openWindow({
					url: url
				});
			} else {
				mui.alert("", $.i18n.prop('noAuthortiy'));
			}
		} else {
			mui.openWindow({
				url: "dahua://login"
			});
		}

	});

});
function openIntegral(){
	var result = false;
	if(!partnerPage.salesIntegralOpen||partnerPage.salesIntegralOpen=='0') {
		mui.alert($.i18n.prop('Scan_message11'),"","OK");
		result=true;
	}else if(!partnerPage.integralOpen||partnerPage.integralOpen=='0') {
		mui.confirm($.i18n.prop('Scan_message1'), $.i18n.prop('Reminder'), [$.i18n.prop('Ingnore'),$.i18n.prop('Apply')], function(e) {
			if(e.index == 1) {
				if(partnerPage.provision=='') {
					mui.ajax("/customer/integral/provision.do", {
						data: {
							"cid": partnerPage.uid,
						},
						type:'post',
						success: function(data) {
							var status = data.status;
							if(status == 1) {								
								partnerPage.provision=data.object.content;
								openIntegralAgreement(data.object.content);
							}
						}
					});
				}else {
					openIntegralAgreement(partnerPage.provision);
				}
			}
		});
		result=true;
	}else if(partnerPage.integralOpen=='1'){
		mui.alert($.i18n.prop('Scan_message8'),"","OK");
		result=true;
	}
	return result;
}
function openIntegralAgreement(msgStr){
	var divHeight = window.screen.availHeight-300;	
	var msg='<div style="height:'+divHeight+'px;text-align:left;position: relative;"><div class="mui-scroll-wrapper"><div class="mui-scroll">'+
	msgStr +
	'</div></div></div>';
	mui.confirm(msg, $.i18n.prop('Agree_and_Accept_Partner_Program_Terms'), [$.i18n.prop('Disagree_and_Quit'),$.i18n.prop('Agree_and_Accept')], function(e) {
		if(e.index == 1) {
			openIntegralPage();
		}
	});
	mui('.mui-scroll-wrapper').scroll();
}
function openIntegralPage(){
	mui.openWindow({
		url:'../integral/open.html'
	});
}